import 'package:flutter/material.dart';
import 'package:flutter_jpf_core/dialog/dialog_loading.dart';
import 'package:flutter_jpf_core/widgets/nodata/nodata_view.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:keyboard_dismisser/keyboard_dismisser.dart';

import '../core_enum.dart';

abstract class CommBasePage extends StatefulWidget {

  //延时默认100毫秒调用initPageData()
  int delayedInitPageDataFunc = 100;

  @override
  State<StatefulWidget> createState() => getPageState();


  CommBaseState getPageState();

}

abstract class CommBaseState<T extends CommBasePage> extends State<T>{
  DialogLoading loadingDialog;
  NoDataView _noDataView;
  bool hasInputView = false;

  @override
  void initState() {
    super.initState();
    initBaseParams();

    Future.delayed(Duration(milliseconds: widget.delayedInitPageDataFunc), () {
      initPageData();
    });
    // 进入页面统计
    String pageName = "${this.runtimeType.toString()}";
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    // 离开页面统计
    String pageName = "${this.runtimeType.toString()}";
  }

  @override
  void showProgressDialog({String msg, bool canCancel = true}){
    if(loadingDialog == null){
      loadingDialog = DialogLoading(context);
    }
    loadingDialog.showLoading(msg: msg);
  }

  @override
  void showToastTip(String tips,{Toast toastLength = Toast.LENGTH_SHORT}) {
    if(tips != null && tips.length>0){
      Fluttertoast.showToast(
          msg: tips ?? "",
          toastLength: toastLength,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.black54,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }

  @override
  void hideProgressDialog(){
    if(loadingDialog != null){
      loadingDialog.hideLoading();
      loadingDialog = null;
    }
  }

  @override
  BuildContext getContext() {
    return context;
  }

  @override
  Widget build(BuildContext context) {

    return hasInputView?
    KeyboardDismisser(
        gestures: [GestureType.onTap, GestureType.onPanUpdateDownDirection],
        child: getViewsWidget()
    )
        :getViewsWidget();
  }

  void hideKeyboard(){
    FocusScope.of(context).requestFocus(FocusNode());
  }

  void setNodataView({String strMsg = "暂无数据",int dataType = NoDataView.nodata}){
    _noDataView = NoDataView(strMsg:strMsg,dataType: dataType ,);
  }
  Widget nodataView(){
    return _noDataView ?? NoDataView(strMsg:"暂无数据",dataType: NoDataView.nodata ,);
  }

  //初始化页面基本参数
  void initBaseParams();

  //初始化页面数据
  void initPageData();

  //页面Views组件
  Widget getViewsWidget();

  //调用接口数据
  void queryServiceData({ServiceType type = ServiceType.Default,bool showAnimail = true}) async{

  }

  //延时关闭页面
  void delayPop({int milliseconds = 1000,Object obj}){
    Future.delayed(Duration(milliseconds: milliseconds), () {
      if(obj != null){
        Navigator.of(context).pop(obj);
      }else{
        Navigator.of(context).pop();
      }
    });
  }

  Object getPageArguments(){
    return ModalRoute.of(context).settings.arguments;
  }
}
